<?php
$oAppconfig = AppconfigHandler::getConfig();
$iClientgroup = @$_REQUEST['iClientgroup'];
$oUsers = UserHandler::getUsers();
$oClientgroups = ClientHandler::getClientgroups();
if (@$_REQUEST['iClientgroup'])
{
	$iClientgroup = $_REQUEST['iClientgroup'];
}
else
{
	$oItem = each($oClientgroups);
	reset($oClientgroups);
	$iClientgroup = $oItem[0];
	unset($oItem);
}
$oClients = ClientHandler::getClients($iClientgroup);
$sAccess = ClientHandler::getAccess($iClientgroup);
$oClientUsers = ClientHandler::getUsers($iClientgroup);

if ($sAccess == 'admin') {
	$sColspan = '3';
}
else {
	$sColspan = '2';
}

$oMenuBar['mnuClientlists'] = array(
	'caption' => msg('Clientlists'),
	'menu' => array(
		array(
			'type' => 'url',
			'caption' => msg('Configure...'),
			'url' => 'javascript: showBox(\'client-groups\'); clearMenu();'
		),
		array(
			'type' => 'sep',
		),
	)
);

$bShowClientlistConfig = (@$oAppconfig['bFreeClientlists'] || $oUsers[$_SESSION['rid']]->bAccountant);
foreach ($oClientgroups as $iID => $oThisClientgroup) {
	if (ClientHandler::getAccess($iID) == 'admin') {
		$bShowClientlistConfig = true;
	}
	$oMenuBar['mnuClientlists']['menu'][] = array(
		'type' => 'bool',
		'caption' => $oThisClientgroup->sName,
		'checked' => ($iID == $iClientgroup),
		'url' => 'index.php?sView=client-list&iClientgroup='.$iID,
	);
}

if (!$bShowClientlistConfig) {
	array_shift($oMenuBar['mnuClientlists']['menu']); // Remove 'Configure...'
	array_shift($oMenuBar['mnuClientlists']['menu']); // Remove separator.
}

?>
<script language="JavaScript" src="js/rotmb.js"></script>
<script language="JavaScript" src="js/rotmb_extra.js"></script>

<form name="form_client_list">
<table class="boxborder" cellspacing="0">
	<tr>
		<td class="boxhandle" colspan="<?php print $sColspan ?>"><?php print msg('Clients') ?></td>
	</tr>
	<tr>
		<td class="control small" colspan="<?php print $sColspan ?>">
			<?php showMenuBar($oMenuBar); ?>
		</td>
	</tr>
<?php
if (count($oClientgroups) > 0) {
	?>
	<tr>
		<td class="control small" colspan="<?php print $sColspan ?>">
	<?php
	if ($sAccess == 'admin') {
		print '<input type="button" value="'.msg('Assignment').'" onClick="showBox(\'client-users\', \'iClientgroup='.$iClientgroup.'\');">&nbsp;';
	}
	print msg('Users').': ';
	$bFirst = true;
	foreach ($oClientUsers as $iThisUser => $sThisAccess) {
		if (!$bFirst) {
			print ', ';
		}
		$bFirst = false;
		print '<a href="index.php?sView=user-view&iUser='.$iThisUser.'">'.$oUsers[$iThisUser]->sUser.'</a> ('.($sThisAccess == 'admin' ? msg('Admin') : msg('User')).')';
	}
	?>
		</td>
	</tr>
	<tr>
	<?php
	if ($sAccess == 'admin')
	{
		?>
		<td class="listtop small center nowrap" style="width: 1px;">
			<a href="javascript: showBox('client-add', 'iClientgroup=<?php print $iClientgroup ?>');"><img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/client-add.png" border="0"></a>
		</td>
		<?php
	}
	?>
		<td class="listtop"><?php print msg('Name') ?>:</td>
		<td class="listtop center"><?php print msg('Worknumber') ?>:</td>
	</tr>
	<?php
	foreach ($oClients as $iID => $oThisClient)
	{
		?>
		<tr>
		<?php
		if ($sAccess == 'admin')
		{
			?>
			<td class="listentity small center nowrap" style="width: 1px;">
				<a href="javascript: showBox('client-delete', 'iClientgroup=<?php print $iClientgroup ?>&iClient=<?php print $iID ?>');"><img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/client-delete.png" border="0"></a>
				<a href="javascript: showBox('client-change', 'iClientgroup=<?php print $iClientgroup ?>&iClient=<?php print $iID ?>');"><img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/client-change.png" border="0"></a>
			</td>
			<?php
		}
		?>
			<td class="listentity"><?php print $oThisClient->sName ?></td>
			<td class="listentity center"><?php print $oThisClient->sSerial ?></td>
		</tr>
		<?php
	}
}
?>
</table>
</form>